import 'dart:math';

import 'package:flutter/material.dart';
import 'package:qimeng/Colorgamedata.dart';
import 'package:qimeng/colorall.dart';

import 'ColorGameItem.dart';

class ColorGame extends StatefulWidget {
  const ColorGame({super.key});

  @override
  State<ColorGame> createState() => _ColorGameState();
}

class _ColorGameState extends State<ColorGame> {
  PageController _controller = PageController();

  int count = 0;
  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    List<ColorGameData> list = [
      ColorGameData(
          color: Color(0xffEE220F),
          img: 'assets/切图/测试-颜色-选项1.png',
          text: '红色',
          tip: 'red'),
      ColorGameData(
          color: Color(0xffFDEA17),
          img: 'assets/切图/测试-颜色-选项-1.png',
          text: '黄色',
          tip: 'yellow'),
      ColorGameData(
          color: Color(0xff1588EB),
          img: 'assets/切图/测试-颜色-选项-2.png',
          text: '蓝色',
          tip: 'blue'),
      ColorGameData(
          color: Color(0xff6CB52E),
          img: 'assets/切图/测试-颜色-选项-3.png',
          text: '绿色',
          tip: 'green'),
      ColorGameData(
          color: Color(0xff7D218D),
          img: 'assets/切图/测试-颜色-选项-5.png',
          text: '紫色',
          tip: 'purple'),
      ColorGameData(
          color: Color(0xffF9A418),
          img: 'assets/切图/测试-颜色-选项-7.png',
          text: '橙色',
          tip: 'orange'),
      ColorGameData(
          color: Color(0xffBF632F),
          img: 'assets/切图/测试-颜色-选项-4.png',
          text: '棕色',
          tip: 'brown'),
      ColorGameData(
          color: Color(0xffFD76AA),
          img: 'assets/切图/测试-颜色-选项-6.png',
          text: '粉色',
          tip: 'pink'),
      ColorGameData(
          color: Color(0xffF9F7F5),
          img: 'assets/切图/测试-颜色-选项-8.png',
          text: '白色',
          tip: 'white'),
      ColorGameData(
          color: Color(0xff0B1323),
          img: 'assets/切图/测试-颜色-选项-9.png',
          text: '黑色',
          tip: 'black'),
    ];
    Set<int> randomNumbers = Set<int>();

    while (randomNumbers.length < 4) {
      int randomNumber = Random().nextInt(list.length - 1) + 1;
      randomNumbers.add(randomNumber);
    }
    int trueColorTest = Random().nextInt(3) + 1;
    List<int> colorTest1 = randomNumbers.toList();
    return Scaffold(
      backgroundColor: Kbgcolor,
      appBar: AppBar(
        elevation: 0,
        leadingWidth: 100,
        scrolledUnderElevation: 0,
        backgroundColor: Kbgcolor,
        toolbarHeight: size.height * 0.1,
        leading: InkWell(
          onTap: () {
            Navigator.pop(context);
          },
          child: Image.asset(
            'assets/切图/Home组件.png',
            scale: 0.6,
            fit: BoxFit.cover,
          ),
        ),
        actions: [Image.asset('assets/切图/组 17.png')],
      ),
      body: Stack(
        children: [
          PageView(
            onPageChanged: (value) {
              print(value);
              setState(() {
                count = value;
              });
            },
            // physics: NeverScrollableScrollPhysics(),
            controller: _controller,
            children: List.generate(list.length + 2, (index) {
              if (index == list.length + 1) {
                return Padding(
                  padding: EdgeInsets.symmetric(horizontal: size.width * 0.03),
                  child: Column(
                    children: [
                      Text(
                        '以下哪个是${list[colorTest1[trueColorTest]].text}的？',
                        style: TextStyle(
                            color: Color(0xffFD7968),
                            fontSize: size.width * 0.1,
                            fontWeight: FontWeight.w900,
                            shadows: [
                              Shadow(
                                  color: Colors.black.withOpacity(0.16),
                                  blurRadius: 6,
                                  offset: Offset(0, 3))
                            ]),
                      ),
                      GridView.count(
                          shrinkWrap: true,
                          crossAxisCount: 2,
                          crossAxisSpacing: 20,
                          mainAxisSpacing: 20,
                          children: List.generate(4, (index) {
                            return InkWell(
                              onTap: () {
                                if (index == trueColorTest) {
                                  Navigator.pop(context);
                                }
                              },
                              child: Container(
                                height: size.width * 0.25,
                                decoration: BoxDecoration(
                                    image: DecorationImage(
                                        image: AssetImage(
                                            list[colorTest1[index]].img)),
                                    borderRadius: BorderRadius.circular(20),
                                    boxShadow: [
                                      BoxShadow(
                                          color: Color(0xffA0A0A0)
                                              .withOpacity(0.16),
                                          blurRadius: 24,
                                          offset: Offset(0, 24)),
                                    ]),
                              ),
                            );
                          }))
                    ],
                  ),
                );
              } else if (index == list.length) {
                return Padding(
                  padding: EdgeInsets.symmetric(horizontal: size.width * 0.03),
                  child: Column(
                    children: [
                      Text(
                        '哪个方块是${list[colorTest1[trueColorTest]].text}？',
                        style: TextStyle(
                            color: Color(0xffFD7968),
                            fontSize: size.width * 0.1,
                            fontWeight: FontWeight.w900,
                            shadows: [
                              Shadow(
                                  color: Colors.black.withOpacity(0.16),
                                  blurRadius: 6,
                                  offset: Offset(0, 3))
                            ]),
                      ),
                      GridView.count(
                          shrinkWrap: true,
                          crossAxisCount: 2,
                          crossAxisSpacing: 20,
                          mainAxisSpacing: 20,
                          children: List.generate(
                            4,
                            (index) => InkWell(
                              onTap: () {
                                if (index == trueColorTest) {
                                  _controller.jumpToPage(count + 1);
                                }
                              },
                              child: Container(
                                height: size.width * 0.25,
                                decoration: BoxDecoration(
                                    color: list[colorTest1[index]].color,
                                    // color: colorTest[index],
                                    borderRadius: BorderRadius.circular(20),
                                    boxShadow: [
                                      BoxShadow(
                                          color: Color(0xffA0A0A0)
                                              .withOpacity(0.16),
                                          blurRadius: 24,
                                          offset: Offset(0, 24)),
                                    ]),
                              ),
                            ),
                          ))
                    ],
                  ),
                );
                ;
              } else {
                return ColorGameItem(
                  size: size,
                  data: list[index],
                );
              }
            }),
          ),
          Visibility(
            visible: count != list.length + 1 && count != list.length,
            child: Positioned(
                bottom: 100,
                right: 0,
                child: Container(
                  width: size.width * 0.8,
                  height: size.width * 0.15,
                  decoration: BoxDecoration(
                    boxShadow: [
                      BoxShadow(
                          color: Colors.black.withOpacity(0.16),
                          offset: Offset(0, 3),
                          blurRadius: 6)
                    ],
                    color: Color(0xffFFE3E2),
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(30),
                        bottomLeft: Radius.circular(30)),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      InkWell(
                        onTap: () {
                          _controller.jumpToPage(list.length);
                        },
                        child: Image.asset('assets/切图/biz-test.png'),
                      ),
                      InkWell(
                        onTap: () {
                          if (count == 0) {
                          } else {
                            _controller.jumpToPage(count - 1);
                          }
                        },
                        child: Image.asset('assets/切图/Left-5.png'),
                      ),
                      InkWell(
                        onTap: () {
                          if (count == list.length - 1) {
                            _controller.jumpToPage(count + 1);
                          } else {
                            _controller.jumpToPage(count + 1);
                          }
                        },
                        child: Image.asset('assets/切图/Left-1.png'),
                      ),
                    ],
                  ),
                )),
          )
        ],
      ),
    );
  }
}
// padding: EdgeInsets.symmetric(horizontal: size.width * 0.03),
